package cn.edu.nit.sys.handle;

import cn.edu.nit.sys.utils.SysRuntimeInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @Author xiaobai
 * @Date 2022/8/31 21:51
 * @Version 1.0.0
 */
@Component
public class TimerHandler {

    @Autowired
    private SysRuntimeInfo sysRuntimeInfo;

    /**
     * 获取硬件使用率（每分钟）
     * @return
     */
    @Async
    public void getRate() {
        Calendar instance = Calendar.getInstance();
        instance.set(Calendar.MINUTE, instance.get(Calendar.MINUTE) + 1);
        instance.set(Calendar.SECOND, 0);
        Date time = instance.getTime();
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                if(instance.get(Calendar.SECOND) == 0) {
                    sysRuntimeInfo.methods(1000L * 60);
                }else {
                    sysRuntimeInfo.methods((60 - instance.get(Calendar.SECOND)) * 1L);
                }
            }
        }, time, 1000L * 60);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                if(instance.get(Calendar.SECOND) == 0) {
                    sysRuntimeInfo.netInfoLoop(1000L * 60);
                }else {
                    sysRuntimeInfo.netInfoLoop((60 - instance.get(Calendar.SECOND)) * 1L);
                }
            }
        }, time, 1000L * 60);
    }
}
